import greenfoot.*;

public class Bird extends MoveBckGr
{
    private int x;
    private int y;
    private int back;      //This variable is a flag that indicates if the man is touching the bird.
    private int level;
    private int distance;           //Score.
    private int direction;          //Right or left (movement).
    private int pictNumber;         //Number of the image in the array.
    private SimpleTimer time;       //The time of each image.
    private SimpleTimer drop;       //Time that the bird avoid the man.
    private SimpleTimer speed;      //Bird's speed.
    private GreenfootImage[] pictures;

    /**
     * Bird Constructor.
     */
    public Bird( int lev)
    {
        time = new SimpleTimer();
        drop = new SimpleTimer();
        speed = new SimpleTimer();
        drop.mark();
        time.mark();
        speed.mark();
        back = 0;
        level = lev;
        direction =- 2;
        pictNumber = 2;
        pictures = new GreenfootImage[4];
        pictures[0] = new GreenfootImage( "Bird_0.png");
        pictures[1] = new GreenfootImage( "Bird_1.png");
        pictures[2] = new GreenfootImage( "Bird_2.png");
        pictures[3] = new GreenfootImage( "Bird_3.png");
        setImage( pictures[ pictNumber]);
    }

    /**
     * Method act.
     */
    public void act() 
    {
        if( time.millisElapsed() > 200)                 //Change of images.
        {
            if( pictNumber  == 0 ||  pictNumber  == 2)
                pictNumber++;
            else
                pictNumber--;
            setImage( pictures[ pictNumber]);
            time.mark();
        }
        if( drop.millisElapsed() > 6000 && back == 1)   //Follows the man again.
            back=0;
        if ( speed.millisElapsed() > 100 && back == 0)  //Follows the man each time limited.
        {
            this.follow();
            speed.mark();
        }
        if( back == 0)                                          //Amount of distance could change.
            distance=super.shift( getX(), getY(), 1, 0, level);
        else
            distance=super.shift( getX(), getY(), 3, 0, level);
    }    

    /**
     * Method follow select the right direction, image and position of the bird following the man.
     */
    public void follow()
    {
        Actor a;
        Sky sky;
        Sun sun;
        Man man;
        x = super.getX();
        y = super.getY();

        World world = getWorld();
        if (level == 1)
        {
            sky = ( Sky)world;
            man = sky.getMan();
        }
        else
        {
            sun = ( Sun)world;
            man = sun.getMan();
        }
        if( x > man.getX())                         //Change the direction.
            direction = -2;
        else
            direction = 2;
        if( y > man.getY())                         //Change the height.
            y--;
        else
            y++;

        if( direction == 2 && ( pictNumber == 2 || pictNumber == 3))    //Change of images.
            pictNumber = 0;
        else
        if( direction == -2 && ( pictNumber == 0 || pictNumber == 1))
            pictNumber = 2;
        setLocation( x + direction, y);
    }

    /**
     * Method back let the man scape for a limited time.
     */
    public void back()
    {
        if( isTouching( Man.class) == true)
        {
            super.getAct( true);
            back = 1;
            direction =- 2;
            drop.mark();
        }
        else
            super.getAct( false);
    }

    /**
     * Method punt return the score.
     */
    public int punt()
    {
        return distance;
    }
}